package com.lgj.auth.code.token;

import com.lgj.core.LoginUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;


/**
 * 增加token携带信息
 * @author: guangjie.liao
 * @Date: 2022/11/17 15:17
 * @Description: 自定义token增强
 */
public class DefaultOauthTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Object obj = authentication.getPrincipal();
        if (obj instanceof LoginUser){
            LoginUser user = (LoginUser) obj;
            Map<String,Object> map = new HashMap<>();
            map.put("userInfo", user);
            ((DefaultOAuth2AccessToken)accessToken).setAdditionalInformation(map);
        }
        return accessToken;
    }
}
